package com.lottery.ruok.lottrey.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.ChoosemanagerAdapter;
import com.lottery.ruok.lottrey.chat.RongCloudChatActivityHelper;
import com.lottery.ruok.lottrey.eventbusObject.BubbleChange;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetRoomAdminResquest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomAdminResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 管理员
* */

public class ChooseManagerActivity extends BaseActivity {


    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.close)
    ImageView close;
    private ChoosemanagerAdapter choosemanagerAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_manager);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);

        init();
    }

    @Override
    public void finish() {
        super.finish();

        overridePendingTransition(0, R.anim.anim_exit_from_bottom);
    }

    private void init() {
        RetrofitBase.getRetrofit().create(GetRoomAdminResquest.class)
                .getResult(UserInfo.getToken(), UserInfo.getRoomId() )
                .compose(RxHelper.<GetRoomAdminResponse>handleResult())
                .subscribe(new RxSubscribe<GetRoomAdminResponse>(this) {
                    @Override
                    protected void _onNext(final GetRoomAdminResponse getRoomAdminResponses) {
                        choosemanagerAdapter = new ChoosemanagerAdapter(ChooseManagerActivity.this, getRoomAdminResponses);
                        listview.setAdapter(choosemanagerAdapter);
                        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                                GetRoomAdminResponse.ListBean item = (GetRoomAdminResponse.ListBean) adapterView.getItemAtPosition(i);
                                RongCloudChatActivityHelper.startPrivateChat(ChooseManagerActivity.this, item.getRy_id(), item.getNickname());

                            }
                        });
                    }

                    @Override
                    protected boolean showDialog() {
                        return false;
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(BubbleChange event) {
        if (choosemanagerAdapter != null) {
            choosemanagerAdapter.notifyDataSetChanged();
        }
    }

    @OnClick(R.id.close)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (choosemanagerAdapter != null) {
            choosemanagerAdapter.notifyDataSetChanged();
        }
    }
}
